package com.bookm.bookm.controller;

import com.bookm.bookm.entity.Result;
import com.bookm.bookm.service.impl.RecorderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/recorder")
public class RecordServlet extends HttpServlet {
    RecorderServiceImpl service = new RecorderServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String key = req.getParameter("key");
        String id = req.getParameter("id");
        String bookId = req.getParameter("bookId");
        PrintWriter out = resp.getWriter();
        switch (key){
            case "getRecordList" :
                getRecorderList(req,out);
                break;
            case  "getReaderRecordList":
                getReaderRecordList(req,out,id);
                break;
            case "addRecord":
                addRecord(req,out,id,bookId);
                break;
            case  "delRecord":
                delRecord(req,out,id,bookId);
                break;
            case  "updateRecord":
                updateRecord(req,out,id,bookId);
                break;
        }
    }


    public void getRecorderList(HttpServletRequest req,PrintWriter out){
        Result result = service.getRecordList();
        out.println(result);
    }

    public void getReaderRecordList(HttpServletRequest req,PrintWriter out,String id){
        Result result = service.getReaderRecordList(id);
        out.println(result);
    }

    public void addRecord(HttpServletRequest req,PrintWriter out,String id,String bookId){
        Result result = service.addRecord(id,bookId);
        out.println(result);
    }

    public void delRecord(HttpServletRequest req,PrintWriter out,String id,String bookId){
        Result result = service.delRecord(id,bookId);
        out.println(result);
    }

    public void updateRecord(HttpServletRequest req,PrintWriter out,String id,String bookId){
        Result result = service.updateRecord(id,bookId);
        out.println(result);
    }
}
